<template>
  <div class="containerWrap">
    <el-container class="container">
      <el-main class="rightMain">
        <el-container>
          <el-header class="dzy-main-header">
            <div class="btn-group-el">
              <btn-group :currentSelectedData="currentSelectedData" @btnClickCallback="btnClickCallback"></btn-group>
            </div>
            <div class="dzy-custom-search-box">
              <div>
                姓名：<el-input size="small" placeholder="请输入姓名" v-model="queryData.name" clearable class="dzy-custom-input"></el-input>
              </div>
              <div>
                手机：<el-input size="small" placeholder="请输入手机" v-model="queryData.phone" clearable class="dzy-custom-input"></el-input>
              </div>
              <div>
                身份证：<el-input size="small" placeholder="请输入身份证" v-model="queryData.idCardNumber" clearable class="dzy-custom-input"></el-input>
              </div>
              <div>
                是否委托人：
                <el-select filterable size="small"  style="width: 125px" clearable v-model="queryData.isClient" placeholder="是否委托人">
                  <el-option :value="1" label="是"></el-option>
                  <el-option :value="0" label="否"></el-option>
                </el-select>
              </div>
              <div>
                <el-button size="small" @click="searchHandleClick" type="primary" icon="el-icon-search">查询</el-button>
              </div>
            </div>
          </el-header>
          <el-main class="tableMain">
            <app-table :page-data="pageData"
                       :table-columns="tableColumns"
                       v-on:handleCheckedRows="handleCheckedRows"
                       v-on:currentChange="handleCurrentChange"/>
            <el-dialog :title="dialogTitle"
                       :visible.sync="dialogFormVisible"
                       :close-on-click-modal="false"
                       class="dialog_style"
                        :before-close="close">
              <div class="formWrapper">
                <el-form :model="formData"
                         :rules="rules"
                         ref="formData"
                         label-width="95px"
                         size="small">
                  <el-divider content-position="left">基本信息</el-divider>
                  <el-row :gutter="15">
                    <el-col :span="12">
                      <el-form-item label="姓名：" prop="name">
                        <el-input placeholder="姓名" v-model="formData.name"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="是否委托人：" prop="isClient">
                        <el-select filterable style="width: 100%;" clearable v-model="formData.isClient" placeholder="状态">
                          <el-option :key="0" :value="1" label="是"></el-option>
                          <el-option :key="1" :value="0" label="否"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="性别：" prop="sex">
                        <el-select filterable style="width: 100%;" clearable v-model="formData.sex" placeholder="性别">
                          <el-option v-for="item in genderOption"
                            :key="item.id"
                            :value="item.value"
                            :label="item.name"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="民族：" prop="nation">
                        <el-select filterable style="width: 100%;" clearable v-model="formData.nation" placeholder="民族">
                          <el-option v-for="item in nationOption"
                            :key="item.id"
                            :value="item.value"
                            :label="item.name"></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="手机：" prop="phone">
                        <el-input placeholder="手机" v-model="formData.phone"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="身份证号：" prop="idCardNumber">
                        <el-input placeholder="身份证号" v-model="formData.idCardNumber"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="户籍地址：" prop="householdAddress">
                        <el-input placeholder="户籍地址" v-model="formData.householdAddress"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="现住地址：" prop="liveAddress">
                        <el-input placeholder="现住地址" v-model="formData.liveAddress"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="12">
                      <el-form-item label="工作单位：" prop="workUnit">
                        <el-input placeholder="工作单位" v-model="formData.workUnit"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item label="备注：" prop="remark">
                        <el-input placeholder="备注" type='textarea' :rows='5' v-model="formData.remark"></el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-form>
              </div>
              <div v-if="!showOnly" slot="footer" class="dialog-footer">
                <el-button @click="cancel('formData')">取 消</el-button>
                <el-button type="primary" @click="save('formData')">提 交</el-button>
              </div>
            </el-dialog>
          </el-main>
        </el-container>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import AppTree from '@/components/common/AppTree'
import BtnGroup from '@/components/BtnGroup'
import AppTable from '@/components/AppTable'
import AppUpload from '@/components/AppUpload'
import {getDictDataOption} from '@/utils/getDictDataOption'
import {
  createDefendant,
  findDefendant,
  updateDefendant,
  deleteDefendant,
  getDefendant
} from '@/http/kehuApi'
export default {
  data () {
    return {
      treeData: [],
      currentSelectedData: [],
      queryData: {},
      pageData: {},
      formData: {},
      genderOption: [],
      nationOption: [],
      dialogTitle: '',
      dialogFormVisible: false,
      showOnly: false,
      tableColumns: [
        { label: '姓名', prop: 'name' },
        { label: '是否委托人', prop: 'isClient', formatter: this.clientFormatter },
        { label: '性别', prop: 'sex', formatter: this.sexFormatter },
        { label: '手机', prop: 'phone' },
        { label: '身份证号', prop: 'idCardNumber' },
        { label: '创建时间', prop: 'createTime', formatter: this.dateFormatter },
        { label: '备注', prop: 'remark' }
      ],
      rules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        isClient: [{ required: true, message: '请选是否委托人', trigger: 'change' }]
      },
      dataPickerBeforeTodayOptions: {
        disabledDate (time) {
        }
      }
    }
  },
  mounted () {
    this.getTableData()
    this.genderOption = getDictDataOption('性别')
    this.nationOption = getDictDataOption('民族')
  },
  components: {
    AppTree,
    BtnGroup,
    AppTable,
    AppUpload},
  methods: {
    // 时间格式化
    dateFormatter (row) {
      if (row && row.createTime) {
        return this.$moment(row.createTime).format('YYYY-MM-DD')
      }
    },
    sexFormatter (row) {
      let name = ''
      for (let index = 0; index < this.genderOption.length; index++) {
        const element = this.genderOption[index]
        if (row.sex === element.value) {
          name = element.name
        }
      }
      return name
    },
    clientFormatter (row) {
      if (row.status === 1) {
        return '是'
      } else if (row.status === 0) {
        return '否'
      }
    },
    btnClickCallback (callbackData) {
      if (callbackData.btnType === 'delete') {
        this.dialogFormVisible = false
        let rows = callbackData.rows
        this.del(rows)
      } else {
        if (callbackData.btnType === 'post') {
        } else {
          let data = callbackData.rows[0]
          this.getSingle(data)
        }
        this.showOnly = callbackData.showOnly
        this.dialogTitle = callbackData.dialogTitle
        this.dialogFormVisible = true
      }
    },
    save (formName) {
      this.$refs[formName].validate((valid) => {
        if (!valid) {
          this.$alert('请填写正确表单格式', '', {
            confirmButtonText: '确定'
          })
          return ''
        } else {
          if (this.formData.id) {
            updateDefendant(this.formData).then(resp => {
              if (resp.code && resp.code === '200') {
                this.getTableData()
                this.cancel(formName)
                this.$message({
                  showClose: true,
                  message: '修改成功',
                  type: 'success'
                })
              }
            })
          } else {
            console.log(this.formData)
            createDefendant(this.formData).then(resp => {
              if (resp.data && resp.code === '200') {
                this.$message({
                  type: 'success',
                  message: '添加成功'
                })
                this.cancel(formName)
                this.getTableData()
              }
            })
          }
        }
      })
    },
    del (rows) {
      if (rows && rows instanceof Array && rows.length > 0) {
        this.$confirm('此操作将永久删除，是否继续？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          rows.forEach(row => {
            deleteDefendant(row).then(resp => {
              if (resp.code && resp.code === '200') {
                this.getTableData()
                this.$message({
                  showClose: true,
                  message: '删除成功',
                  type: 'success'
                })
              }
            })
          })
        })
      }
    },
    cancel (formName) {
      this.formData = {}
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    close () {
      this.formData = {}
      this.dialogFormVisible = false
    },
    getTableData () {
      let param = this.queryData
      findDefendant(param).then(resp => {
        if (resp.data && resp.code === '200') {
          this.pageData = resp.data
        }
      })
    },
    // 搜索按钮操作
    searchHandleClick () {
      this.queryData.offset = 0
      this.getTableData()
    },
    handleCheckedRows (rows) {
      if (rows && rows instanceof Array && rows.length > 0) {
        this.currentSelectedData = rows
      } else {
        this.currentSelectedData = []
      }
    },
    handleCurrentChange (val) {
      this.queryData.offset = (val - 1) * 10
      this.getTableData()
    },
    // 查看单条活动分类详细信息
    getSingle (data) {
      getDefendant(data).then(resp => {
        if (resp.data && resp.code === '200') {
          this.formData = resp.data
        }
      })
    }
  }
}
</script>
<style scoped>
  .containerWrap {
    padding:10px;
    height: 100%;
    box-sizing: border-box
  }
  .container {
    height: 100%;
  }
</style>
